#include "cpphelper.h"

#include <afxwin.h>

namespace MCCommon
{

wchar_t* 
CppHelper::Ansi2Unicode
(
    const char* astr
)
{
    //  Get the transformed length.
    int wlen = ::MultiByteToWideChar( CP_ACP, NULL, astr, -1, NULL, 0 );
   
    wchar_t* wstr = new wchar_t[ wlen ];

    // Transform to Unicode.
    ::MultiByteToWideChar( CP_ACP, NULL, astr, -1, wstr, wlen );

    return wstr;
}

char* 
CppHelper::Unicode2Ansi
(
    const wchar_t* wstr
)
{
    int alen = ::WideCharToMultiByte( CP_ACP, NULL, wstr, -1, NULL, 0,
        NULL, NULL );

    char* astr = new char[ alen ];
    
    ::WideCharToMultiByte( CP_ACP, NULL, wstr, -1, astr, alen,
        NULL, NULL );

    return astr;
}

char* 
CppHelper::Unicode2UTF8
(
    const wchar_t* wstr
)
{
    int ulen = ::WideCharToMultiByte( CP_UTF8, NULL, wstr, -1, NULL, 0,
        NULL, NULL );

    char* ustr = new char[ ulen ];

    ::WideCharToMultiByte( CP_UTF8, NULL, wstr, -1, ustr, ulen,
        NULL, NULL );

    return ustr;
}

} // end of namespace MCCommon